package com.ftg.midamall.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ftg.midamall.admin.dto.UmsMenuVoo;
import com.ftg.midamall.admin.entity.UmsMenu;
import com.ftg.midamall.admin.service.IUmsMenuService;
import com.ftg.midamall.admin.service.impl.UmsMenuServiceImpl;
import com.ftg.midamall.admin.vo.UmsMenuTreeVo;
import com.ftg.midamall.admin.dto.UmsMenuVo;
import com.ftg.midamall.comm.vo.CodeStatusEnum;
import com.ftg.midamall.comm.vo.ConstUtil;
import com.ftg.midamall.comm.vo.RespMessUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单
 *
 * @author qjw
 * @version 1.0
 * @date 2023/10/24 15:41
 */
@RestController
@Api(tags = "菜单")
@RequestMapping("/ums/menu/umsMenu")
public class UmsMenuController {

    @Resource
    IUmsMenuService iUmsMenuService;


    @Resource
    UmsMenuServiceImpl umsMenuService;

    @GetMapping("/getMenuTree")
    @ApiOperation("展示菜单树")
    public RespMessUtil<List<UmsMenuTreeVo>> MenuTree(){
        return iUmsMenuService.allMenuForTree();
    }

    @GetMapping("/showMenuByAdminId")
    @ApiOperation("依据adminid查找menu")
    public RespMessUtil<List<UmsMenuTreeVo>> showMenuByAdminId(
            @ApiParam(name = ConstUtil.TOKEN_NAME, value = ConstUtil.TOKEN_NAME_ZH, required = true) @RequestHeader(value = ConstUtil.TOKEN_NAME) String token
    ) {
        return iUmsMenuService.showMenuByAdminId(token);
    }

    @ApiOperation(value = "添加菜单")
    @PostMapping("/addmenu")
    public RespMessUtil<Integer> addmenu(@RequestBody UmsMenuVo ge) {
        int n = umsMenuService.insertmenu(ge);
        return n > 0 ? RespMessUtil.insertOk() : RespMessUtil.insertFailure();
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/deletemenu/{id}")
    public RespMessUtil<Integer> deletemenu(@PathVariable("id") int id) {
        int n = umsMenuService.deletemenu(id);
        return n > 0 ? RespMessUtil.deleteOk() : RespMessUtil.deleteFailure();
    }

    @ApiOperation(value = "批量删除菜单")
    @DeleteMapping("/deletemenus")
    public RespMessUtil<Integer> deletemenus(
            @ApiParam(name="ids",value="主键",required = true)@RequestParam("ids") List<Integer> ids) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS,umsMenuService.deletemenus(ids));
    }

    @GetMapping("/showAllWothName")
    @ApiOperation("分页查看信息")
    public RespMessUtil<IPage> showRole(
            @ApiParam(value = "页码", name = "pageSize", required = true) @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize,
            @ApiParam(value = "显示条数", name = "pageLimit", required = true) @RequestParam(value = "pageLimit", defaultValue = "10") Integer pageLimit,
            @ApiParam(value = "角色名称", name = "name") @RequestParam(value = "name", required = false) String roleName

    ){
        return umsMenuService.showAllWithPage(pageSize,pageLimit,roleName);
    }

    @ApiOperation(value = "根据名称修改菜单")
    @PutMapping("/updatamenu")
    public RespMessUtil<Integer> updatemenu(@RequestBody UmsMenuVo se) {
        int n = umsMenuService.updatemenu(se);
        return n>0 ? RespMessUtil.insertOk() : RespMessUtil.insertFailure();
    }

}